package smartt.styy.auth.controller;

import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import smartt.styy.auth.constants.RedisConstants;
import smartt.styy.auth.constants.ServerConstants;
import smartt.styy.auth.service.SmsCodeSender;
import smartt.styy.auth.service.VcodeManager;
import smartt.styy.common.constant.RestCodeConstants;
import smartt.styy.common.exception.BaseException;
import smartt.styy.common.msg.ObjectRestResponse;

@Api(tags = { "短信发送相关接口" })
@RestController
@RequestMapping("/vCode")
@Slf4j
public class VCodePoint {

	@Autowired
	private VcodeManager vcodeManager;
	
	@Autowired
	private SmsCodeSender smsCodeSender;
	
	@ApiOperation(value = "短信发送", notes = "发送短信验证码（测试默认短信：123456）")
	@PostMapping(value = "/sendSms")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "phoneNum", value = "用户手机号码", required = true, dataType = "String", paramType="query"),
		@ApiImplicitParam(name = "smsType", value = "短信发送类型（1：手机注册发送，2：手机短信登录发送(如：第三方二次登录)，3：手机短信重置密码）", required = true, dataType = "int", paramType="query")
	})
	public ObjectRestResponse<Object> sendSms(@RequestParam(value="phoneNum",required = true)String phoneNum,
			@RequestParam(value="smsType",required = true)Integer smsType){
		log.info("[短信发送相关接口]-[短信发送] start ...");
		ObjectRestResponse<Object> objectRestResp = new ObjectRestResponse<Object>();
		try {
			//生成短信验证码 短信验证码
			String smsCode ="123456";//vcodeManager.generateVcode();
			
			if(!Pattern.matches(ServerConstants.REGMOBILE, phoneNum)) {
				log.error("[短信发送相关接口]-[发送短信验证码]--电话号码不正确");
				objectRestResp.setStatus(ServerConstants.ERROR_60050);
				objectRestResp.setMessage(ServerConstants.ERROR_MSG_60050);
				objectRestResp.setRel(false);
				return objectRestResp;
			}
			
			if(smsType == null){
				log.error("[短信发送相关接口]-[发送短信验证码]--短信发送类型不正确！");
				objectRestResp.setStatus(ServerConstants.ERROR_60060);
				objectRestResp.setMessage(ServerConstants.ERROR_MSG_60060);
				objectRestResp.setRel(false);
				return objectRestResp;
			}
			//1：手机注册发送，2：手机短信登录(如：第三方二次登录)发送，3：手机短信重置密码
			String sendVcodeRedisPerfix = new String("");
			if(smsType == RedisConstants.REDIS_SEND_TYPE_1){
				sendVcodeRedisPerfix = RedisConstants.REDIS_REGISTER_PREFIX;
			}
			if(smsType == RedisConstants.REDIS_SEND_TYPE_2){
				sendVcodeRedisPerfix = RedisConstants.REDIS_ACCOUNT_PREFIX;
			}
			if(smsType == RedisConstants.REDIS_SEND_TYPE_3){
				sendVcodeRedisPerfix = RedisConstants.REDIS_RESETPWD_PREFIX;
			}
			if(StringUtils.isEmpty(sendVcodeRedisPerfix)){
				objectRestResp.setStatus(ServerConstants.ERROR_60060);
				objectRestResp.setMessage(ServerConstants.ERROR_MSG_60060);
				objectRestResp.setRel(false);
				return objectRestResp;
			}
			
			// 保存验证码	
			vcodeManager.saveVcode(sendVcodeRedisPerfix,phoneNum, smsCode, 5, TimeUnit.MINUTES);	
			
			//模拟手机发送，此时调用短信服务商接口
	        smsCodeSender.send(phoneNum,smsCode);
				
		} catch (Exception e) {
			log.error("[短信发送相关接口]-[发送短信验证码]--电话号码不正确");
			throw new BaseException("[用户注册]-[手机号码注册]--数据插入失败-异常，错误message:"+e.getMessage(),RestCodeConstants.THROW_ERROR_CODE);
		}
			
		objectRestResp.setStatus(RestCodeConstants.SUCCESS_CODE);
		objectRestResp.setMessage("发送成功！");
		objectRestResp.setRel(true);
		return objectRestResp;
	}

}
